<template>
  <div class="dashboard-container" v-loading="loading">
    <div class="app-container">
      <el-card class="tree-card">
        <tree-tools :tree-node="company" :isRoot="true" />
        <el-tree :data="departs" :props="defaultProps" default-expand-all>
          <tree-tools
            slot-scope="obj"
            :tree-node="obj.data"
            @delDepts="getDepartments"
            @addDepts="addDepts"
            @editDepts="editDepts"
          />
        </el-tree>
      </el-card>
    </div>
    <add-dept
      ref="addDept"
      :show-dialog.sync="showDialog"
      :tree-node="node"
      @addDepts="getDepartments"
    />
  </div>
</template>
  
  <script>
import TreeTools from "./components/tree-tools";
import { getDepartments } from "@/api/departments";
import { tranListToTreeData } from "@/utils";
import AddDept from "./components/add-dept";
export default {
  components: {
    TreeTools,
    AddDept,
  },

  data() {
    return {
      loading: false,
      showDialog: false,
      company: {},
      departs: [],
      node: null,
      defaultProps: {
        label: "name",
      },
    };
  },
  created() {
    this.getDepartments();
  },
  methods: {
    async getDepartments() {
      this.loading = true;
      const result = await getDepartments();
      this.departs = tranListToTreeData(result.depts, "");
      this.company = { name: result.companyName, manager: "负责人", id: "" };
      this.loading = false;
    },
    addDepts(node) {
      this.showDialog = true;
      this.node = node;
    },
    editDepts(node) {
      this.showDialog = true;
      this.node = node;
      this.$refs.addDept.getDepartDetail(node.id);
    },
  },
  computed: {},
};
</script>
  
  <style scoped>
.tree-card {
  padding: 30px 140px;
  font-size: 14px;
}
</style>